package com.itheima.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtils {
    //给好密钥和有效时间
    private static String signKey = "SVRIRUlNQQ==fe";
    private static Long expire = 43200000L;


    //生成JWT令牌
    public static String creatJwt(Map<String,Object> claims){

        String jwt = Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256, signKey)
                .setExpiration(new Date(System.currentTimeMillis() + expire))
                .compact();
        return jwt;
    }

    /**
     * 解析令牌
     * @param jwt JWT令牌
     * @return JwT第二部分负载 payload中存储的内容
     */
    public static Claims parseJWT(String jwt){
        Claims claims = null;
        try {
            claims = Jwts.parser()
                    .setSigningKey(signKey)
                    .parseClaimsJws(jwt)
                    .getBody();
        } catch (Exception e) {

        }
        return claims;
    }
}
